# 用于编译STM32F10x芯片的标准库为二进制库

include $(RULES)/defs.common.mk
include $(RULES)/defs.stm32.inc.mk


# 编译成的二进制库的库名
LIB_BASE_NAME = stm32

# 编译外设库的源文件
PERIPH_SOURCE = $(wildcard $(STM32_PERIPH_DIR)/src/*.c)
PERIPH_OBJS = $(patsubst %.c, $(MOD_OBJS_DIR)/%.o, $(notdir $(PERIPH_SOURCE)))
VPATH += $(STM32_PERIPH_DIR)/src

# 编译ARM的 CMSIS 层源文件
SYS_F10X_SOURCE = $(STM32_CORE_DIR)/Device/ST/STM32F10x/system_stm32f10x.c
SYS_F10X_OBJS= $(patsubst %.c, $(MOD_OBJS_DIR)/%.o, $(notdir $(SYS_F10X_SOURCE)))
VPATH += $(STM32_CORE_DIR)/Device/ST/STM32F10x

# 用于GCC编译器 ARM Cortex-M3 小端的数学库
ARMCM3_MATH_BASE_NAME = arm_cortexM3l_math
ARMCM3_MATH_NAME = lib$(ARMCM3_MATH_BASE_NAME).a
ARMCM3_MATH_PATH = $(STM32_CORE_DIR)/Lib/$(ARMCM3_MATH_NAME)

MOD_OBJS += $(PERIPH_OBJS)
MOD_OBJS += $(SYS_F10X_OBJS)


all: dep_dirs $(LIBS)
	@cp $(ARMCM3_MATH_PATH) $(LIBDIR)
	@echo -n " -l$(LIB_BASE_NAME) -l$(ARMCM3_MATH_BASE_NAME) " >> $(LDFLAGS_FILE3)
	@echo "Building $(LIBDIR)/$(LIBS) Done."

dep_dirs:
	@echo "Use Optimization Level $(OPTLVL) to build $(LIBS) ..."
	@$(MKDIR) $(MOD_OBJS_DIR)

$(LIBS): $(PERIPH_OBJS) $(SYS_F10X_OBJS)
	@$(AR) cr $(LIBDIR)/$@ $(MOD_OBJS)

$(PERIPH_OBJS): $(MOD_OBJS_DIR)/%.o: %.c
	@$(CC) $(CFLAGSLIB) -o $@ $<

$(SYS_F10X_OBJS): $(MOD_OBJS_DIR)/%.o: %.c
	@$(CC) $(CFLAGSLIB) -o $@ $<

clean:
	@echo "Clean all objs..."
	@$(RM) $(MOD_OBJS)

distclean: clean
	@echo "Clean $(LIBS)"
	@$(RM) $(LIBDIR)/$(LIBS)
	@$(RM) $(LIBDIR)/$(ARMCM3_MATH_NAME)



.PHONY: all dep_dirs $(LIBS) clean distclean

